package com.example.firstboot.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.firstboot.entity.CheckList;
import com.example.firstboot.service.CheckService;
import com.example.firstboot.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class CheckController {
    @Autowired
    private CheckService checkService;

    @RequestMapping("/CheckList")
    @ResponseBody
    public JsonResult queryCheckList(){
        List<CheckList> list=checkService.queryCheckList();
        System.out.println(list);
        if(list!=null){
            return new JsonResult(list,"请求成功");
        }else {
            return new JsonResult(-1,"查询失败");
        }
    }

    @PostMapping("/saveCheckResult")
    @ResponseBody
    public JsonResult saveCheckResult(@RequestBody JSONObject json){
        String account=(String) json.get("account");
        String checkName=(String) json.get("checkName");
        String checkResult=(String) json.get("checkResult");
        int id=checkService.findCheckIdByName(checkName);
        System.out.println("==========checkid======="+id);
        String checkId=Integer.toString(id);
        int i=checkService.saveCheckResult(account,checkId,checkResult);
        if(i>0){
            return new JsonResult();
        }else {
            return new JsonResult(-1,"数据保存失败");
        }
    }
}
